//
// Copyright (c) 2004 BroadVision, Inc. All rights reserved.
//
// This software is copyrighted.  Under the copyright laws, this software
// may not be copied, in whole or in part, without prior written consent
// of BroadVision, Inc. or its assignees.  This software is provided under
// the terms of a license between BroadVision and the recipient, and its
// use is subject to the terms of that license.
//
// This software may be protected by one or more U.S. and International
// patents.  Certain applications of BroadVision One-To-One software are
// covered by U.S. patent 5,710,887.
//
// TRADEMARKS: BroadVision and BroadVision One-To-One are registered
// trademarks of BroadVision, Inc., in the United States and the European
// Community, and are trademarks of BroadVision, Inc., in other
// countries.  The BroadVision logo, is a trademark of BroadVision, Inc.,
// in the United States and other countries. Additionally, IONA and Orbix
// are trademarks of IONA Technologies, Ltd.  RSA, MD5, and RC2 are
// trademarks of RSA Data Security, Inc.
//

package com.broadvision.manage.tools.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ExceptionHandler;
import org.apache.struts.config.ExceptionConfig;
import com.broadvision.basic.log.LogLevel;
import com.broadvision.manage.tools.ToolsLog;

public class ToolsExceptionHandler extends ExceptionHandler {

  private String priority;
  
  /**
   * Override {@link super#execute} to log the exception in the tools
   * log. A priority can be specified. In Struts 1.2, remove this method
   * because it will automatically call {@link #logException}.
   */
  public ActionForward execute(java.lang.Exception ex,
                               ExceptionConfig ae,
                               ActionMapping mapping,
                               ActionForm formInstance,
                               HttpServletRequest request,
                               HttpServletResponse response)
                        throws javax.servlet.ServletException {

    if (ae instanceof ToolsExceptionConfig) {
      setPriority(((ToolsExceptionConfig) ae).getPriority());
    }
    logException(ex);

    return super.execute(ex, ae, mapping, formInstance, request, response);
  }
  
  /**
   * Logging priority (or level)
   */
  public void setPriority(String priority) {
    this.priority = priority;
  }

  /**
   * Returns the logging priority.
   */
  public String getPriority() {
    return this.priority;
  }
  
  /**
   * Logs the exception in the tools log.
   */
  protected void logException(java.lang.Exception e) {
    ToolsLog log = new ToolsLog(getClass());
    LogLevel level = LogLevel.ERROR;
    if (getPriority() != null) {
      level = (LogLevel)LogLevel.toLevel(getPriority());
    }

    log.log(level, "logException", e);
    setPriority(null);
  }
}

